package com.sg.service.biz.component.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.sg.common.util.RedisUtil;
import com.sg.dto.biz.component.common.AnalyzeDataSetCompletionDto;
import com.sg.dto.biz.component.req.*;
import com.sg.dto.biz.component.res.*;
import com.sg.service.base.component.MCustomFields3Service;
import com.sg.service.base.component.MOmsConfItemOptionRelationshipService;
import com.sg.service.base.component.MOmsMessageLocationRemindService;
import com.sg.common.exception.Assert;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.stream.Collectors;

/**
 * @author ：
 * @version 1.0
 * @date ：
 */
@Service
public class DivineDataServiceImpl
        implements com.wicket.okrcomponent.biz.service.DivineDataService {

    @Resource
    private RedisUtil redisUtil;
    //@Resource
    //private MSpecMethordTableService mSpecMethordTableService;
    @Resource
    private NbDivineData nbDivineData;
    @Resource
    private MCustomFields3Service mCustomFields3Service;
    @Resource
    private MOmsMessageLocationRemindService mOmsMessageLocationRemindService;
    @Resource
    private MOmsConfItemOptionRelationshipService mOmsConfItemOptionRelationshipService;
//@Resource
    //private MCustomFieldsService mCustomFieldsService;

    /**
     * D2接收输出参数(公共)[4196]
     * gen by moon at 2/5/2024, 8:55:46 PM
     */
    @Trace(operationName = "D2接收输出参数(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public QueryOutputParametersDetailComRespDto queryOutputParametersDetailCom(QueryOutputParametersDetailComReqDto reqDto) {


        QueryOutputParametersDetailRespDto receptionServiceRes_1 = null;
//步骤0: M2接收输出参数 - queryOutputParametersDetail
        //ModelCode: receptionService
        QueryOutputParametersDetailRespDto receptionServiceRes = null;
        QueryOutputParametersDetailReqDto receptionServiceReq = new QueryOutputParametersDetailReqDto();
        if (reqDto != null) {
            receptionServiceReq.setIsOpenAudit(reqDto.getIsOpenAudit());//SimpleFieldAssign//sourceId:464733_1
            receptionServiceReq.setIsOpenEvaWork(reqDto.getIsOpenEvaWork());//SimpleFieldAssign//sourceId:464734_1
        }
        if (reqDto != null && reqDto.getMessageNumList() != null && !CollectionUtil.isEmpty(reqDto.getMessageNumList())) {
            receptionServiceReq.setMessageNumList(reqDto.getMessageNumList().stream().map(item -> BeanUtil.toBean(item, MessageNumDto.class)).collect(Collectors.toList()));//objList-to-objLists//sourceId:1292018_1
        }
        if (reqDto != null && reqDto.getAccountCapitalSourceList() != null && !CollectionUtil.isEmpty(reqDto.getAccountCapitalSourceList())) {
            receptionServiceReq.setAccountCapitalSourceList(reqDto.getAccountCapitalSourceList().stream().map(item -> BeanUtil.toBean(item, AccountCapitalSourceDto.class)).collect(Collectors.toList()));//objList-to-objLists//sourceId:1533194_1
        }

        /*M2接收输出参数[4197]  用于特殊方法接收上游入参。 */

        receptionServiceRes = nbDivineData.queryOutputParametersDetail(receptionServiceReq);


        receptionServiceRes_1 = receptionServiceRes;

        QueryOutputParametersDetailComRespDto retData = new QueryOutputParametersDetailComRespDto();
        if (receptionServiceRes_1 != null) {
            retData.setObjectName(receptionServiceRes_1.getObjectName());//SimpleFieldAssign//sourceId:259056_1
            retData.setIsOpenAudit(receptionServiceRes_1.getIsOpenAudit());//SimpleFieldAssign//sourceId:464735_1
            retData.setIsOpenEvaWork(receptionServiceRes_1.getIsOpenEvaWork());//SimpleFieldAssign//sourceId:464736_1
            retData.setMessageNumList(receptionServiceRes_1.getMessageNumList().stream().map(item -> BeanUtil.toBean(item, MessageNumDto.class)).collect(Collectors.toList()));//objList-to-objLists//sourceId:1292021_1
            retData.setAccountCapitalSourceList(receptionServiceRes_1.getAccountCapitalSourceList().stream().map(item -> BeanUtil.toBean(item, AccountCapitalSourceDto.class)).collect(Collectors.toList()));//objList-to-objLists//sourceId:1533197_1
        }


        return retData;
    }

    /**
     * D2获取上一周期类型服务(公共)[3514]
     * gen by moon at 9/21/2022, 4:22:25 AM
     */
    @Trace(operationName = "D2获取上一周期类型服务(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ObtainLastCycleTypeComRespDto obtainLastCycleTypeCom(ObtainLastCycleTypeComReqDto reqDto) {


        ObtainLastCycleTypeRespDto getLastCycleTypeCodeRes_1 = null;
//步骤0: M2获取上一周期类型(特殊方法) - obtainLastCycleType
        //ModelCode: getLastCycleTypeCode
        ObtainLastCycleTypeRespDto getLastCycleTypeCodeRes = null;
        ObtainLastCycleTypeReqDto getLastCycleTypeCodeReq = new ObtainLastCycleTypeReqDto();
        if (reqDto != null) {
            getLastCycleTypeCodeReq.setCurrentCycleType(reqDto.getCurrentCycleType());//sourceId:152047_1
            getLastCycleTypeCodeReq.setCycleTypeList(reqDto.getCycleTypeList());//sourceId:152048_1
        }

        /*M2获取上一周期类型(特殊方法)[3513]   */
        Assert.isNull(getLastCycleTypeCodeReq.getCurrentCycleType(), "D2获取上一周期类型服务(公共)-M2获取上一周期类型(特殊方法)-当前周期类型不能为空", false);
        getLastCycleTypeCodeRes = nbDivineData.obtainLastCycleType(getLastCycleTypeCodeReq);


        getLastCycleTypeCodeRes_1 = getLastCycleTypeCodeRes;

        ObtainLastCycleTypeComRespDto retData = new ObtainLastCycleTypeComRespDto();
        if (getLastCycleTypeCodeRes_1 != null) {
            retData.setResultCycleType(getLastCycleTypeCodeRes_1.getResultCycleType());//sourceId:152052_1
        }


        return retData;
    }

    /**
     * D2两个时间对比大小(公共)[3297]
     * gen by moon at 4/7/2024, 3:03:19 PM
     */
    @Trace(operationName = "D2两个时间对比大小(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public CheckTimeScopeSearchComRespDto checkTimeScopeSearchCom(CheckTimeScopeSearchComReqDto reqDto) {


        CheckTimeScopeSearchRespDto timeEfficiencyCheckRes_1 = null;
//步骤0: M2检查时间范围（特殊方法） - checkTimeScopeSearch
        //ModelCode: timeEfficiencyCheck
        CheckTimeScopeSearchRespDto timeEfficiencyCheckRes = null;
        CheckTimeScopeSearchReqDto timeEfficiencyCheckReq = new CheckTimeScopeSearchReqDto();
        if (reqDto != null) {
            timeEfficiencyCheckReq.setCurrentTime(reqDto.getCurrentTime());//SimpleFieldAssign//sourceId:127301_1
            timeEfficiencyCheckReq.setCompareTime(reqDto.getCompareTime());//SimpleFieldAssign//sourceId:127316_1
        }

        /*M2检查时间范围[2472]  入参开始时间（参照值）、结束时间（比较值）两个字段，计算比较值与参照值大小，输出两个时间比较结果之前（小于）、当前（等于）、之后（大于） */
        Assert.isNull(timeEfficiencyCheckReq.getCurrentTime(), "D2两个时间对比大小(公共)-M2检查时间范围-当前时间不能为空", false);
        Assert.isNull(timeEfficiencyCheckReq.getCompareTime(), "D2两个时间对比大小(公共)-M2检查时间范围-比较时间不能为空", false);
        timeEfficiencyCheckRes = nbDivineData.checkTimeScopeSearch(timeEfficiencyCheckReq);


        timeEfficiencyCheckRes_1 = timeEfficiencyCheckRes;

        CheckTimeScopeSearchComRespDto retData = new CheckTimeScopeSearchComRespDto();
        if (timeEfficiencyCheckRes_1 != null) {
            retData.setTimeCompareResult(timeEfficiencyCheckRes_1.getTimeCompareResult());//SimpleFieldAssign//sourceId:127306_1
        }


        return retData;
    }

    /**
     * D2执行分析数据集完成情况(公共)[5520]
     * gen by moon at 4/6/2023, 3:58:28 PM
     */
    @Trace(operationName = "D2执行分析数据集完成情况(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ImplementAnalyzeDataSetCompletionComRespDto implementAnalyzeDataSetCompletionCom(ImplementAnalyzeDataSetCompletionComReqDto reqDto) {


        ImplementAnalyzeDataSetCompletionRespDto analyzeDataSetCompletionRes_1 = null;
//步骤0: M2执行多数据集取并集（特殊方法） - implementUnionMultipleDataSets
        //ModelCode: collectionsMergeData
        ImplementUnionMultipleDataSetsRespDto collectionsMergeDataRes = null;
        ImplementUnionMultipleDataSetsReqDto collectionsMergeDataReq = new ImplementUnionMultipleDataSetsReqDto();
        if (reqDto != null) {
            //simpleList-to-objLists
            for (String item : reqDto.getDataSetsListOne()) {
                DataSetsListOneDto oneItem = new DataSetsListOneDto();
                if (reqDto != null) {
                    oneItem.setCustomField1(item);//SimpleFieldAssign//sourceId:139616_2
                }

                collectionsMergeDataReq.getDataSetsListOne().add(oneItem);
            }//sourceId:545318_1
//simpleList-to-objLists
            for (String item : reqDto.getDataSetsListTwo()) {
                DataSetsListTwoDto oneItem = new DataSetsListTwoDto();
                if (reqDto != null) {
                    oneItem.setCustomField1(item);//SimpleFieldAssign//sourceId:176101_2
                }

                collectionsMergeDataReq.getDataSetsListTwo().add(oneItem);
            }//sourceId:545319_1
        }

        /*M2执行多数据集取并集（特殊方法）[5319]  入参多个数据集，取并集 */

        collectionsMergeDataRes = nbDivineData.implementUnionMultipleDataSets(collectionsMergeDataReq);
        Assert.isTrue(collectionsMergeDataRes == null || collectionsMergeDataRes.getUnionMultipleDataSetsList() == null || CollectionUtil.isEmpty(collectionsMergeDataRes.getUnionMultipleDataSetsList()) || collectionsMergeDataRes.getUnionMultipleDataSetsList().size() == 0, "找不到数据，系统异常", false);


//步骤1: M2执行分析数据集完成情况（特殊方法） - implementAnalyzeDataSetCompletion
        //ModelCode: analyzeDataSetCompletion
        ImplementAnalyzeDataSetCompletionRespDto analyzeDataSetCompletionRes = null;
        ImplementAnalyzeDataSetCompletionReqDto analyzeDataSetCompletionReq = new ImplementAnalyzeDataSetCompletionReqDto();
        if (collectionsMergeDataRes != null && collectionsMergeDataRes.getUnionMultipleDataSetsList() != null && !CollectionUtil.isEmpty(collectionsMergeDataRes.getUnionMultipleDataSetsList())) {
            analyzeDataSetCompletionReq.setAnalyzeDataSetCompletionList(//objList-to-objLists
                    collectionsMergeDataRes.getUnionMultipleDataSetsList().stream().map(item -> {
                        AnalyzeDataSetCompletionDto elm = new AnalyzeDataSetCompletionDto();
                        if (item != null) {
                            elm.setIsComplete(item.getCustomField1());//SimpleFieldAssign//sourceId:139620_2
                        }
                        return elm;
                    }).collect(Collectors.toList()));//objList-to-objLists//sourceId:545321_1
        }

    /*M2执行分析数据集完成情况（特殊方法）[5521]  入参数据。数据集记录中有是否完成字段，根据该字段来分析分子分母。
分母=数据集中数据总条数，分子=数据集中是否完成为是的记录数之和。 */

        analyzeDataSetCompletionRes = nbDivineData.implementAnalyzeDataSetCompletion(analyzeDataSetCompletionReq);


        analyzeDataSetCompletionRes_1 = analyzeDataSetCompletionRes;

        ImplementAnalyzeDataSetCompletionComRespDto retData = new ImplementAnalyzeDataSetCompletionComRespDto();
        if (analyzeDataSetCompletionRes_1 != null) {
            retData.setComFloatField1(analyzeDataSetCompletionRes_1.getComFloatField1());//SimpleFieldAssign//sourceId:545404_1
            retData.setComFloatField2(analyzeDataSetCompletionRes_1.getComFloatField2());//SimpleFieldAssign//sourceId:545405_1
            retData.setIsComplete(analyzeDataSetCompletionRes_1.getIsComplete());//SimpleFieldAssign//sourceId:545408_1
        }


        return retData;
    }

    /**
     * D2执行取数据集最大值加一(公共)[9706]
     * gen by moon at 6/2/2024, 1:02:19 AM
     */
    @Trace(operationName = "D2执行取数据集最大值加一(公共)")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ImplementMaxOfCollectionsAddOneComRespDto implementMaxOfCollectionsAddOneCom(ImplementMaxOfCollectionsAddOneComReqDto reqDto) {


        ObtainMaxOfCollectionsAddOneRespDto maxOfMultipleNumAddOneRes_1 = null;
//步骤0: M2-获取数据集最大值加一（特殊方法） - obtainMaxOfCollectionsAddOne
        //ModelCode: maxOfMultipleNumAddOne
        ObtainMaxOfCollectionsAddOneRespDto maxOfMultipleNumAddOneRes = null;
        ObtainMaxOfCollectionsAddOneReqDto maxOfMultipleNumAddOneReq = new ObtainMaxOfCollectionsAddOneReqDto();
        if (reqDto != null) {
            maxOfMultipleNumAddOneReq.setMaxOfMultipleNumList(reqDto.getMaxOfMultipleNumList());//list-field-assign//sourceId:1836475_1
        }

        /*M2-获取数据集最大值加一（特殊方法）[4617]  用于取数据集中最大的数值，在最大数值基础上加一，输出 */

        maxOfMultipleNumAddOneRes = nbDivineData.obtainMaxOfCollectionsAddOne(maxOfMultipleNumAddOneReq);


        maxOfMultipleNumAddOneRes_1 = maxOfMultipleNumAddOneRes;

        ImplementMaxOfCollectionsAddOneComRespDto retData = new ImplementMaxOfCollectionsAddOneComRespDto();
        if (maxOfMultipleNumAddOneRes_1 != null) {
            retData.setOrderNumber(maxOfMultipleNumAddOneRes_1.getOrderNumber());//SimpleFieldAssign//sourceId:1836478_1
        }


        return retData;
    }
    //
}
